package App.Utility;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;

/**
 * Created by xw on 2018/11/16.
 */
public class SecurityHash {

    public static String getHash(String originalString) throws Exception {

        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        // Send a sample text to hash.
        byte[] encodedhash = digest.digest(originalString.getBytes(StandardCharsets.UTF_8));

        // Get the hashed string.
        return bytesToHex(encodedhash);
    }

    private static String bytesToHex(byte[] hash) {
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < hash.length; i++) {
            String hex = Integer.toHexString(0xff & hash[i]);
            if (hex.length() == 1) hexString.append('0');
            hexString.append(hex);
        }
        return hexString.toString();
    }

    public static String getPwdSecurity(String passWord,String salt) throws  Exception
    {
        return getHash(passWord + salt);
    }




}
